package model

import (
	"fmt"
	"time"
)

type Advisers struct {
	Id int32 `xorm:"pk autoincr"`
	DepartmentId int32
	DepartmentName string
	Name string
	Age int
	Sex string
	Position string
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Departments struct {
	Id int32 `xorm:"pk autoincr"`
	SubDepartmentId int32
	Name string
	CreatedAt time.Time
	UpdatedAt time.Time
}

type CallLog struct {
	Id int64 `xorm:"pk autoincr"`
	AdviserId int32
	CustomerId int32
	CallType int
	TalkTime int64
	CreatedAt time.Time
	UpdatedAt time.Time
}

type TotalAdviserCallLog struct {
	CustomerName string
	DepartmentName string
	CallNumber int64
	TalkTime1s int64
	TalkTime3s int64
	TalkTime60s int64
	TalkTime180s int64
	SumTalkTime int64
	AverageTalkTim int64
}

func NewCallLog() *CallLog{
	return new(CallLog)
}

func (c *CallLog)GetSubsidiary(totalAsList *[]*Advisers, sd int32) error {
	var asList []*Advisers
	err := x.Join("INNER", "departments", "departments.id = advisers.department_id").
		Where("advisers.department_id = ?", sd).
		//Where("sub_department_id like ?", "%"+sd).
		//And("talk_time > ?", 1).And("talk_time < ?", 3).
		//OrderBy("call_log.adviser_id").
		Find(&asList)
	if err != nil {
		return err
	}
	*totalAsList = append(*totalAsList, asList...)

	var dpList []*Departments
	err = x.Where("sub_department_id = ?", sd).
		Find(&dpList)

	if err != nil {
		return err
	}
	if dpList != nil {
		for _, v := range dpList {
			err := c.GetSubsidiary(totalAsList, v.Id)
			if err != nil {
				return err
			}
		}
	}
	return nil
}

func (c *CallLog)QueryCallLog(t *QueryTotalCall)([]*TotalAdviserCallLog, int, error){
	var totalAsList []*Advisers

	var tacList []*TotalAdviserCallLog

	var session = x.NewSession()
	var session1s = x.NewSession()
	var session3s = x.NewSession()
	var session60s = x.NewSession()
	var session180s = x.NewSession()
	var sessionCallNumber = x.NewSession()
	var sessionSumTalkTime = x.NewSession()

	if t.CallStartDate != ""{
		session = session.Where("created_at >= ?", t.CallStartDate)
		session1s = session1s.Where("created_at >= ?", t.CallStartDate)
		session3s = session3s.Where("created_at >= ?", t.CallStartDate)
		session60s = session60s.Where("created_at >= ?", t.CallStartDate)
		session180s = session180s.Where("created_at >= ?", t.CallStartDate)
		sessionCallNumber = sessionCallNumber.Where("created_at >= ?", t.CallStartDate)
		sessionSumTalkTime = sessionSumTalkTime.Where("created_at >= ?", t.CallStartDate)
	}

	if t.CallrEndDate != ""{
		session = session.And("created_at <= ?", t.CallrEndDate)
		session1s = session1s.And("created_at <= ?", t.CallrEndDate)
		session3s = session3s.And("created_at <= ?", t.CallrEndDate)
		session60s = session60s.And("created_at <= ?", t.CallrEndDate)
		session180s = session180s.And("created_at <= ?", t.CallrEndDate)
		sessionCallNumber = sessionCallNumber.And("created_at <= ?", t.CallrEndDate)
		sessionSumTalkTime = sessionSumTalkTime.And("created_at <= ?", t.CallrEndDate)
	}

	if t.CallType != ""{
		session = session.And("call_type = ?", t.CallType)
		session = session.And("call_type = ?", t.CallType)
		session1s = session1s.And("call_type = ?", t.CallType)
		session3s = session3s.And("call_type = ?", t.CallType)
		session60s = session60s.And("call_type = ?", t.CallType)
		session180s = session180s.And("call_type = ?", t.CallType)
		sessionCallNumber = sessionCallNumber.And("call_type = ?", t.CallType)
		sessionSumTalkTime = sessionSumTalkTime.And("call_type = ?", t.CallType)
	}

	if t.DepartmentId != 0 {
		err := c.GetSubsidiary(&totalAsList, t.DepartmentId)
		if err != nil {
			return nil, 0, nil
		}
	}

	fmt.Println(totalAsList)
	if totalAsList != nil {
		for _, v := range totalAsList {
			var tac TotalAdviserCallLog

			totalCallNumber, err := sessionCallNumber.Where("adviser_id = ?", v.Id).Count(new(CallLog))
			if err != nil {
				return nil, 0, nil
			}
			tac.CallNumber = totalCallNumber

			totalSumTalkTime, err := sessionSumTalkTime.Where("adviser_id = ?", v.Id).SumInt(new(CallLog), "talk_time")
			if err != nil {
				return nil, 0, nil
			}
			tac.SumTalkTime = totalSumTalkTime/60

			if tac.CallNumber == 0{
				tac.AverageTalkTim = 0
			}else {
				tac.AverageTalkTim = tac.SumTalkTime/tac.CallNumber
			}

			tac.DepartmentName = v.DepartmentName

			tac.CustomerName= v.Name

			total1s, err := session1s.Where("talk_time > ?", 1).And("talk_time <= ?", 3).And("adviser_id = ?", v.Id).Count(new(CallLog))
			if err != nil {
				return nil, 0, nil
			}
			tac.TalkTime1s = total1s

			total3s, err := session3s.Where("talk_time > ?", 3).And("talk_time <= ?", 60).And("adviser_id = ?", v.Id).Count(new(CallLog))
			if err != nil {
				return nil, 0, nil
			}
			tac.TalkTime3s = total3s

			total60s, err := session60s.Where("talk_time > ?", 60).And("talk_time <= ?", 180).And("adviser_id = ?", v.Id).Count(new(CallLog))
			if err != nil {
				return nil, 0, nil
			}
			tac.TalkTime60s = total60s

			total180s, err := session180s.Where("talk_time > ?", 180).And("adviser_id = ?", v.Id).Count(new(CallLog))
			if err != nil {
				return nil, 0, nil
			}
			tac.TalkTime180s = total180s

			tacList = append(tacList, &tac)
		}
	}

	totals := len(tacList)

	if totals < t.PageSize*t.PageNo && totals > (t.PageNo-1)*t.PageSize{
		return tacList[(t.PageNo-1)*t.PageSize:], totals, nil
	}

	if totals <= (t.PageNo-1)*t.PageSize{
		return nil, 0, nil
	}

	return tacList[(t.PageNo-1)*t.PageSize:t.PageNo*t.PageSize], totals, nil

}


